;(function($){
	// define TCTree
	$.Tree=$.Widget.extend({
		init: function(treeObject){
			this.treeEl=$(treeObject.treeNode);
			this.treeData=treeObject.treeData||[];
			this.checkable=treeObject.checkable===true?true:false;
			this.treeMap=this.genTreeData(this.treeData);
			this.contextMenu=null;
			if(treeObject.contextMenu&&$.fn.popup){this.initContextMenu(treeObject.contextMenu);}
			this.selectedNode=null;
			this.dragNode=null;
		},show: function(){
			var html=this.genTreeHtml(this.treeData,this.treeMap);
			this.treeEl.empty().append(html);
			this.bind();
		},initContextMenu:function(menuData){
			this.contextMenu=new $.Popup(menuData.items,menuData.callback);
		},getContextMenu:function(){
			return this.contextMenu;
		},setContextMenu:function(menu){
			this.contextMenu=menu?menu:this.contextMenu; return this;
		},bind: function(){
			this.treeEl.disableSelection();
			if(this.contextMenu){
				this.treeEl.bind($.browser.opera&&document.body.oncontextmenu?'mousedown':'contextmenu',this,function(event){
					var tree=event.data;
					tree.getContextMenu().show(event);
					return !tree.getContextMenu().getEl().is(':visible');
				});
			}
			this.treeEl.bind('click',this,function(event){
				var el=$(event.srcElement||event.target), elclass=el.get(0).className, tree=event.data;
				if(tree.isCheckable()&&el.is('.checkable')){
					var index=el.is('.icon_check0')?1:el.is('.icon_check1')?2:el.is('.icon_check2')?3:1;
					el.removeClass('icon_check'+(index-1)).addClass('icon_check'+index%3);
				}else if(el.is('.nodeText')){
					if(tree.selectedNode){ tree.selectedNode.removeClass('tree-node-selected');}
					tree.selectedNode=el.addClass('tree-node-selected');
				}else if(el.is('.icon_tree')){
					var isHide=el.parent().is('.hide-sub');
					el.parent().removeClass(isHide?'hide-sub':'show-sub').addClass(!isHide?'hide-sub':'show-sub')
					  .parent().removeClass(isHide?'hide-sub':'show-sub').addClass(!isHide?'hide-sub':'show-sub');
				}
			});
			this.treeEl.bind('mousedown',this,function(event){
				var $el=$(event.srcElement||event.target);
				var tree=event.data;
				if($el.is('.icon_tree')){
					var p=$el.parents('.treeNode:first').children('.icon_tree:first');
					var pos=$el.position();
					var of=$el.offset();
					tree.getEl().data('mousedown',{el:p,pos:{top:pos.top-of.top,left:pos.left-of.left}});
					$('#test1').val('top:'+pos.top+',left:'+pos.left+',clientY:'+event.clientY+',clientX:'+event.clientX+',offsetTop:'+of.top+',offsetLeft:'+of.left);
					return false;
				}
			});
			this.treeEl.bind('mousemove',this,function(event){
				var $el=$(event.srcElement||event.target);
				var tree=event.data;
				var dragData=tree.getEl().data('mousedown');
				if(dragData){
					var shadow=tree.dragger();
					if(!shadow){
						tree.dragger($('<span class="treeNode dragable"></span>').append(dragData.el.nextAll().clone()).appendTo(tree.getEl()));
						shadow=tree.dragger();
					}
					if($el.parent().hasClass('treeNode')){
						shadow.addClass('enableDrop');
					}else{
						shadow.removeClass('enableDrop');
					}
					var pos = shadow.position();
					shadow.css({top:(event.clientY+dragData.pos.top+5)+'px',left:(event.clientX+dragData.pos.left+5)+'px'});
				}
			});
			this.treeEl.bind('mouseup',this,function(event){
				var $el=$(event.srcElement||event.target);
				var tree=event.data;
				tree.dragger(null);
				tree.getEl().removeData('mousedown');
			});
		},isCheckable: function(){
			return this.checkable;
		},dragger: function($el){
			if($el===null||$el){
				if(this.dragNode){this.dragNode.remove();}
				this.dragNode=$el;
			}
			return this.dragNode;
		},selectNode: function($el){
			if($el.is('.nodeText')){
				if(this.selectedNode){ this.selectedNode.removeClass('tree-node-selected');}
				this.selectedNode=$el.addClass('tree-node-selected');
			}
		},getEl: function(){
			return this.treeEl;
		},getData: function(){
			return this.treeData;
		},genTreeData: function(treeData,treeMap,parentId){
			var me = arguments.callee;
			var data=treeMap||{};
			var tree=treeData||[];
			parentId=parentId||0;
			for(var i=0;i<tree.length;i++){
				data[tree[i].id]=tree[i];//{parentId:,expand:是否展开,checkable:是否可选,checkValue:是否选择,isLeaf:是否子结点,data:原数据}
				me(tree[i].children,data,tree[i].id);
			}
			return data;
		},genTreeHtml: function(treeData,treeMap,id,treeIcons,hasNext){
			var html='';
			if(!treeData){return html;}
			if(!treeMap){return html;}
			var data = treeData;
			var icons=this.icons;
			treeIcons=treeIcons||'';
			if(id==undefined||id==null){}else{
				data=(treeMap[id]||{}).children||[];
				treeIcons=treeIcons.concat('<button class="icon_indent',hasNext,' EN"></button>');
			}
			for(var i=0;i<data.length;i++){
				var next=i<(data.length-1)?' has-next':' no-next', child=(data[i].children||[]).length>0?' has-child':' no-child', show=data[i].exppand?' show-sub':' hide-sub';
				var nodes=data[i].exppand?this.genTreeHtml(data[i].children,treeMap,data[i].id,treeIcons,next):'';
				html=html.concat('<div class="treeRow',show,'"><span class="treeNode',show,child,next,'"><span class="treeIndent">',treeIcons,'</span><button class="icon_tree EN"></button><button class="icon_folder"></button>',this.isCheckable()?'<button class="icon_check0 checkable"></button>':'','<span class="nodeText">',data[i].text,'</span></span><div class="subTree">',nodes,'</div></div>');
			}
			if(id==undefined||id==null){
				html=''.concat('<div id="',$.getIdSeed(),'" class="treeRow show-sub"><span class="treeNode show-sub has-child"><span class="treeIndent"></span><span class="nodeText"></span></span><div class="subTree">',html,'</div></div>');
			}
			return html;
		},destroy:function(){
			$.each('click mousedown mousemove mouseup mousedown contextmenu'.split(' '),function($el){
				$el.unbind(this);
			},this.treeEl);
		}
	});
	// end of define TCTree
})(jQuery);